package com.ecloud.hbase.controller;

import com.ecloud.common.JsonResult;
import com.ecloud.hbase.entities.TdbControlPresentHbaseTable;
import com.ecloud.hbase.entities.TdbDailyHbaseTable;
import com.ecloud.hbase.entities.TdbEnergyPresentHbaseTable;
import com.ecloud.hbase.service.HbaseService;
import com.ecloud.hbase.service.HbaseTdbService;
import com.ecloud.trend.entities.TdbDaily;
import com.ecloud.trend.vo.TdbEnvironmentPresentVo;
import com.ecloud.trend.vo.TdbPresentVo;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiImplicitParam;
import io.swagger.annotations.ApiImplicitParams;
import io.swagger.annotations.ApiOperation;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;
import springfox.documentation.annotations.ApiIgnore;

import java.util.List;
import java.util.Map;

/**
 * @author Squbi
 */
@Api("Hbase Controller")
@RestController
public class HbaseController {

    @Autowired
    private HbaseService hbaseService;

    @Autowired
    private HbaseTdbService hbaseTdbService;

    @ApiOperation("上传数据到库")
    @ApiImplicitParams({
            @ApiImplicitParam(name = "dailyMap", value = "每日的数据", required = true),
            @ApiImplicitParam(name = "rowKey", value = "行键 建议为日期", required = true)
    })
    @PostMapping("/hbase/daily/{rowKey}")
    public JsonResult postDaily(@RequestBody Map<String, String> dailyMap, @PathVariable("rowKey") String rowKey) {
        TdbDailyHbaseTable table = new TdbDailyHbaseTable(dailyMap);
        hbaseService.setData(table, rowKey);
        return JsonResult.success();
    }

    @ApiOperation("从库中获取json数据")
    @GetMapping("/hbase/daily/json/{rowKey}")
    @ApiImplicitParam(name = "rowKey", value = "行键 建议为日期", required = true)
    public JsonResult getDailyJson(@PathVariable String rowKey) {
        TdbDailyHbaseTable data = hbaseService.getData(TdbDailyHbaseTable.class, rowKey);
        return JsonResult.success(data);
    }

    @ApiOperation("从库中获取数据")
    @GetMapping("/hbase/daily/{rowKey}")
    @ApiImplicitParam(name = "rowKey", value = "行键 建议为日期", required = true)
    public JsonResult getDaily(@PathVariable String rowKey) {
        List<TdbDaily> data = hbaseTdbService.getDailyAndParse(rowKey);
        return JsonResult.success(data);
    }

    @ApiOperation("表是否存在")
    @ApiImplicitParam(name = "tableName", value = "表名", required = true)
    @GetMapping("/hbase/exist/{tableName}")
    public JsonResult existTable(@PathVariable String tableName) {
        boolean b = hbaseService.isTableExist(tableName);
        return JsonResult.success(b);
    }

    @ApiOperation("强行停用并删除表")
    @ApiImplicitParam(name = "tableName", value = "表名", required = true)
    @DeleteMapping("/hbase/{tableName}")
    public JsonResult deleteTable(@PathVariable String tableName) {
        hbaseService.deleteTable(tableName);
        return JsonResult.success();
    }

    @ApiOperation("传入能量实时数据")
    @PostMapping("/hbase/present/energy")
    public JsonResult postEnergyPresent(@RequestBody List<TdbPresentVo> tdbPresentVos) {
        hbaseTdbService.setBuildingPresentData(TdbEnergyPresentHbaseTable.class, tdbPresentVos);
        return JsonResult.success();
    }

    @ApiIgnore
    @ApiOperation("获取所有能量实时数据")
    @ApiImplicitParam(name = "tableName", value = "表名", required = true)
    @GetMapping("/hbase/present/energy/{buildingName}")
    public JsonResult getEnergyAllPresent(@PathVariable String buildingName) {
        Map<String, List<TdbPresentVo>> data = hbaseTdbService.getBuildingPresentAllData(TdbEnergyPresentHbaseTable.class, buildingName);
        return JsonResult.success(data);
    }

    @ApiOperation("获取指定时间戳能量实时数据")
    @ApiImplicitParams({
            @ApiImplicitParam(name = "buildingName", value = "建筑名", required = true),
            @ApiImplicitParam(name = "keyTime", value = "时间戳", required = true)
    })
    @GetMapping("/hbase/present/energy/{buildingName}/{keyTime}")
    public JsonResult getEnergyPresent(@PathVariable("buildingName") String buildingName, @PathVariable("keyTime") String keyTime) {
        Map<String, List<TdbPresentVo>> data = hbaseTdbService.getBuildingPresentData(TdbEnergyPresentHbaseTable.class, buildingName, keyTime);
        return JsonResult.success(data);
    }

    @ApiOperation("上传控制实时数据")
    @PostMapping("/hbase/present/control")
    public JsonResult postControlPresent(@RequestBody List<TdbPresentVo> tdbPresentVos) {
        hbaseTdbService.setBuildingPresentData(TdbControlPresentHbaseTable.class, tdbPresentVos);
        return JsonResult.success();
    }

    @ApiIgnore
    @ApiOperation("获取所有控制实时数据")
    @ApiImplicitParam(name = "buildingName", value = "建筑名", required = true)
    @GetMapping("/hbase/present/control/{buildingName}")
    public JsonResult getControlAllPresent(@PathVariable String buildingName) {
        Map<String, List<TdbPresentVo>> data = hbaseTdbService.getBuildingPresentAllData(TdbControlPresentHbaseTable.class, buildingName);
        return JsonResult.success(data);
    }

    @ApiOperation("获取指定时间戳的能量实时数据")
    @ApiImplicitParams({
            @ApiImplicitParam(name = "buildingName", value = "建筑名", required = true),
            @ApiImplicitParam(name = "keyTime", value = "时间戳", required = true)
    })
    @GetMapping("/hbase/present/control/{buildingName}/{keyTime}")
    public JsonResult getControlPresent(@PathVariable("buildingName") String buildingName, @PathVariable("keyTime") String keyTime) {
        Map<String, List<TdbPresentVo>> data = hbaseTdbService.getBuildingPresentData(TdbControlPresentHbaseTable.class, buildingName, keyTime);
        return JsonResult.success(data);
    }

    @ApiOperation("上传环境实时数据")
    @PostMapping("/hbase/present/environment")
    public JsonResult postEnvironmentPresent(@RequestBody List<TdbEnvironmentPresentVo> environmentVos) {
        hbaseTdbService.setEnvironmentPresentData(environmentVos);
        return JsonResult.success();
    }

    @ApiOperation("获取指定时间戳的环境实时数据")
    @ApiImplicitParam(name = "keyTime", value = "时间戳", required = true)
    @GetMapping("/hbase/present/environment/{keyTime}")
    public JsonResult getEnvironmentPresent(@PathVariable String keyTime) {
        List<TdbEnvironmentPresentVo> data = hbaseTdbService.getEnvironmentPresentData(keyTime);
        return JsonResult.success(data);
    }
}
